<template>
  <forms ref="forms" :config="config" @submit:forms="onsubmit" :btns="btns">
    <template v-slot:config_form="{row}">

      <text_more :name="row.name" v-model:value="row.value" :placeholder="row.placeholder"></text_more>
    </template>
  </forms>
</template>

<script>
import {ajax} from "@/js/api_tool.js";
import {iframe} from "@/js/tool.js";

export default {
  props: ['__iframe'],
  data(){
    let id = this.$get('id'), btns = undefined;
    let type_map = this.field_type(this.field_type.CAN_CONFIG, 'label');

    let name_map = {
      'extension_extension_title1': '推广中心菜单1的标题',
      'extension_extension_title2': '推广中心菜单2的标题',
      'extension_extension_title3': '推广中心菜单3的标题',
      'extension_extension_title4': '推广中心菜单4的标题',
    }

    let forms_config = {
      group: {label:'配置组',name:'group',type:'hidden',config:[],tips:'配置所属分组', value:'personalization_param'},
      name: {label:'配置项',name:'name',type:'select_one', config:name_map,tips:'配置名，字母加数字，获取/设定配置的依据，谨慎命名'},
      tips: {label:'配置说明',name:'tips',type:'hidden',tips:'配置说明，提示'},
      type: {label:'表单组件',name:'type',type:'hidden',tips:'',value:''},
      config: {label:'组件配置',name:'config',type:'hidden',tips:''},
      value: {label:'配置值',name:'value',type:'text_one',config:''},
    }
    if (id){
      forms_config.name.is_info = true;
    }
    if (this.$get('is_info')){
      s94Mjs.each(forms_config, function (row){ row.is_info = true; });
      btns = [];
    }
    return {
      id: id,
      btns: btns,
      config: forms_config,
      name_map: name_map,
    }
  },
  methods: {
    onsubmit(formData){
      let url = this.id ? `/admin/config/edit_extend?id=`+this.id : '/admin/config/add_extend';
      ajax({url:url, method:'POST', data:formData}, (res)=>{
        if (this.__iframe){
          iframe.exe('config/index_extend', '$refs.list_url.list');
          iframe.hide('config/edit_extend');
          if (!this.id) this.$refs['forms'].init();
        }else {
          this.$router.push('index_extend');
        }
      }, (res)=>{
        if (res.errors) this.$refs['forms'].errors = res.errors;
      })
    }
  },
  watch: {
    'config.name.value'(v){
      this.config.tips.value = this.name_map[v] || "";

      let type = "text_one";
      let config = "";
      this.config.type.value = type;
      this.config.config.value = config;

      this.config.value.type = type;
      this.config.value.config = config;
    },
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;
    if (this.id) {
      ajax({url:'/admin/config/info_extend?id='+this.id, cache:true}, (res)=>{
        this.$refs['forms'].init(res.data);
        return false;
      }, ()=>{
        if (this.__iframe){
          iframe.remove('edit');
        }else {
          this.$router.push('index');
        }
      })
    }else {
      this.config.type.value = 'text_one';
    }
  }
}
</script>
